package cn.zkw.controller.back;

import cn.zkw.service.CommentService;
import cn.zkw.util.split.SplitPageUtil;
import cn.zkw.vo.Comment;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.omg.CORBA.Object;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Controller
public class BackCommentController {
    @Autowired
    CommentService commentService;

    /**
     * 评论列表
     * @param request
     * @return
     */
    @RequestMapping("/back/comment")
    public ModelAndView toComment(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView("/back/comment");
        modelAndView.addObject("li_number","3");


        SplitPageUtil spu = new SplitPageUtil(request,"");//分页查询 --split
        int allRecorders = commentService.allCommentCount(); //文章总数 --split
        Integer currentPage = spu.getCurrentPage();  //当前页 --split
        Integer lineSize = 8;
        List<Comment> commentList = commentService.splitComment((currentPage-1)*lineSize);
        this.setPageSplitValue("/back/comment",spu,allRecorders,request);// --split
        request.setAttribute("commentList",commentList);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping(value = "/back/comment/get",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    public Comment getComment(Integer comment_id){
        Comment comment = commentService.selectCommentById(comment_id);
        return comment;
    }

    /**
     * 批量删除
     * @param request
     * @param checkbox
     * @return
     */
    @RequestMapping("/back/comment/delete")
    public String deleteCommentBySet(HttpServletRequest request,int[] checkbox){
        List<Integer> list = new ArrayList<>();
        for (int i=0;i<checkbox.length;i++){
            list.add(checkbox[i]);
        }
        int result = commentService.deleteCommentSet(list);
        if(result>0){
            request.getSession().setMaxInactiveInterval(3);
            request.getSession().setAttribute("message","删除了"+result+"条记录");
        }else {
            request.getSession().setMaxInactiveInterval(3);
            request.getSession().setAttribute("message","删除失败,请重试");
        }
        return "redirect:/back/comment";
    }

    //设置split分页参数，向页面传输 --split
    public  void setPageSplitValue(String url, SplitPageUtil spu, Integer allRecorders,HttpServletRequest request) {
        request.setAttribute("url",url);
        request.setAttribute("cp", spu.getCurrentPage());
        request.setAttribute("ls", spu.getLineSize());
        request.setAttribute("col", spu.getColumn());
        request.setAttribute("wk", spu.getKeyWord());
        request.setAttribute("allRecorders", allRecorders);
    }

}
